package cn.whit.blog.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.whit.blog.domain.Tblogtype;
import cn.whit.blog.service.TblogtypeService;
import cn.whit.blog.util.BeanFactory;
import cn.whit.blog.util.DynamicQuery;
import cn.whit.blog.util.MyBeanUtils;
import cn.whit.blog.util.PageModel;
import cn.whit.blog.util.Parameter;
import cn.whit.blog.util.PropertyUtil;

public class TblogtypeServlet extends BaseServlet {
 
	private static final long serialVersionUID = -332647398886000045L;

	TblogtypeService tblogtypeService = (TblogtypeService) BeanFactory.createObject("tblogtypeService");
	
	
	//批量删除数据
	public String delete(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		System.out.println(request.getParameter("ids"));
		String ids = request.getParameter("ids");
		
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		
		if(ids != null) {
			String[] split = ids.split(",");
			for(String s: split) {
				tblogtypeService.deleteById(s);
			}
			writer.write("删除成功".toString());
			return null;
		}
		writer.write("删除失败，请联系管理员".toString());
		return null;
	}
	
	public String addUI(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return "/jsp/blogtype/addUI.jsp";
	}
	
	public String show(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String num = request.getParameter("num");
		if (num == null) {
			num = "1";
		}
		// 该博客的评论的分页处理 获取当前页
		int curNum = Integer.parseInt(num);
		int pageSize = 5;
		
		System.out.println("id=" + id);
		
		Tblogtype tblogtype = tblogtypeService.findById(id);
		
		request.setAttribute("item", tblogtype);
	  
		String picpath = PropertyUtil.getProperty("picpath");
		request.setAttribute("picpath", picpath);
		return "/jsp/blogtype/show.jsp";
	}
	
	
	//管理员后台
	public String list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// num是传入的当前页
		String num = request.getParameter("num");
		String titleSelected = request.getParameter("titleSelected");
		System.out.println(titleSelected);
		if (num == null) {
			num = "1";
		}
		// 获取当前页
		int curNum = Integer.parseInt(num);
		int pageSize = 5;
		DynamicQuery query = new DynamicQuery();
		if (titleSelected != null && !("".equals(titleSelected))) {
			// 查询参数如果要验证数据类型等，在这里执行
			query.addParameter(new Parameter("title", "like", "%" + titleSelected + "%"));
			request.setAttribute("title", titleSelected);
		}
		
		PageModel pm = tblogtypeService.findByPageAndQuery(curNum, pageSize, query);
		
		request.setAttribute("page", pm);
		return "/jsp/blogtype/list.jsp";
	}


	// 新增 
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Tblogtype tblogtype = MyBeanUtils.populate2(Tblogtype.class, request.getParameterMap());
 
		tblogtypeService.save(tblogtype);
		 
		response.sendRedirect(request.getContextPath()+"/TblogtypeServlet?method=list");
		return null;
	}

}
